<?php 
/*
**模型：话题（类似分类）
**inc/class.cates.php
**By QINIAO
*/
class qiCate{
	var $db;
	
	function qiCate($dbhandle)
	{
		$this->db = $dbhandle;
	}
		
	//显示所有话题
	function getCate($page = 1, $prePageNum)
	{
	$start_limit = !empty($page) ? ($page - 1) * $prePageNum : 0;
	$limit = $prePageNum ? "LIMIT $start_limit, $prePageNum" : '';
	$res = $this->db->query("select * from ".DB_PREFIX."cates order by cTime desc $limit");
	$cates = array();
	while($row = $this->db->fetch_array($res))
	{
	$cates[] = $row;
	}
	return $cates;
	}
		
		
	//获取一条话题
	function getOneCate($cid)
	{
	$res = $this->db->query("select * from ".DB_PREFIX."cates where id=$cid");
	$row = $this->db->fetch_array($res);
	if($row < 1)
	{
	redirect('index.php', 1, '不好意思，你查看的话题不存在!3秒钟后跳转到首页！'); 
	}
	elseif($row)
	{
	$cateDate = array(
	'cname' => htmlspecialchars($row['cname'])
	);
	return $cateDate;
	}else{
	return false;
	}
	
	}
	
	//获取最新10条话题
	function getNewCate()
	{
	$res = $this->db->query("select * from ".DB_PREFIX."cates order by cTime desc limit 10");
	while($row = $this->db->fetch_array($res))
	{
	echo "<li><a href='topic.php?id=".$row['id']."'>".$row['cname']."</a></li>";
	}
	}
	
	//更新（更改）话题
	function updateCates($cateDate,$cid)
	{
	$Item = array();
	foreach($cateDate as $key => $date)
	{
		$Item[] = "$key='$date'";
	}
	$upStr = implode(',',$Item);
	$this->db->query("UPDATE ".DB_PREFIX."cates  SET  $upStr WHERE  id=$cid");
	}
	
	//删除话题
	function deleteCate($cid)
	{
	$this->db->query("DELETE FROM ".DB_PREFIX."cates where id=$cid"); 
	}
	
	//获取指定话题的名字
	function getCateName($cid)
	{
	if($cid > 0)
	{
	$res = $this->db->query("SELECT cname FROM ". DB_PREFIX ."cates WHERE id = $cid");
	$row = $this->db->fetch_array($res);
	$cateName = htmlspecialchars($row['cname']);
	}else 
	{
	$cateName = '未分类';
	}
	return $cateName;
	}
	//获取指定话题 1，今日，2，本周，3，官方，4，推荐
	function getSetCate($cset)
	{
	$res = $this->db->query("select * from ".DB_PREFIX."cates where cset = $cset");
	while($row = $this->db->fetch_array($res))
	{ 
	echo "<li><a href='topic.php?id=".$row['id']."'>".$row['cname']."</a></li>";
	}
	}
	//获取话题数
	function getCateNum($virtue, $setvirtue)
	{
	$where = 'where '.$virtue.'='.$setvirtue.'';
	$res = $this->db->query("SELECT * FROM ".DB_PREFIX."cates $where" );
	$CateNum = $this->db->num_rows($res);
	return $CateNum;
	}
	//添加话题
	function addCate($userid,$cname,$ctime,$cset)
	{
	$sql="INSERT INTO ".DB_PREFIX."cates (uid,cname,cTime,cset) VALUES ('$userid','$cname','$ctime','$cset')";
	$this->db->query($sql);
	}
	
	}

?>